package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

/**
 * 老师 ---备授课--成品备课包查看--删除单个资源
 * 
 */
@CrossOrigin
@RestController
public class LecturePrePackDelResController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/del/{packId}/res/{resId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "packId") String packId, @PathVariable(value = "resId") String resId) {

        String userId = getProfile(request, response).getId();
        String userNick = getProfile(request, response).getUserNick();
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolName = getProfile(request, response).getSchoolName();
        YxyLecturePrePack lecturePrePack = yxyLecturePrePackRepository
                .findByIdAndUserIdAndSchoolIdAndIsDeleteNot(packId, userId, schoolId, 1);
        Assert.notNull(lecturePrePack, "备课包不存在");
        List<Res> resList = lecturePrePack.getResources().stream()
                .filter(x -> !StringUtils.equals(x.getResourceId(), resId)).collect(Collectors.toList());
        lecturePrePack.setResources(resList);
        lecturePrePack.setReferencUserId(userId);
        lecturePrePack.setUserNick(userNick);
        lecturePrePack.setSchoolNick(schoolName);
        yxyLecturePrePackRepository.save(lecturePrePack);
        YxyApiRespModel apiRespModel = new YxyApiRespModel();
        apiRespModel.setMessage("备课包删除资源成功");
        return apiRespModel;
    }

}
